#include "ui-renderer.h"
#include "../../global/view.h"
#include "../../gfx/gfx-mgr.h"

/**
 * -1,1     0,1      1,1
 *
 * 0,-1     0,0      0,1
 *
 * -1,-1    0,-1     1,-1
 */

UIRenderer::UIRenderer(std::string rendererId)
{
    this->_rendererId = rendererId;
    GfxMgr::getInstance()->addUIObject(this->_rendererId);
}

/**
 * @brief 更新渲染器的着色器
 *
 * @param shaderPath 着色器路径
 */
void UIRenderer::updateShader(std::string shaderPath)
{
    if (this->_shaderPath == shaderPath)
    {
        return;
    }
    this->_shaderPath = shaderPath;
    GfxMgr::getInstance()->updateUIObjectShader(this->_rendererId, this->_shaderPath);
}
void UIRenderer::updateVertexs(float x, float y, float anchorX, float anchorY, float width, float height)
{
    width = width <= 0 ? 1 : width;
    height = height <= 0 ? 1 : height;
    // 计算四个角的局部坐标（相对于锚点）
    float left = -anchorX * width;
    float right = (1 - anchorX) * width;
    float bottom = -anchorY * height;
    float top = (1 - anchorY) * height;
    // 世界坐标
    float ltX = x + left;
    float ltY = y + top; // Vulkan: Y轴向下，所以 top 是更大的Y值
    float lbX = x + left;
    float lbY = y + bottom;
    float rbX = x + right;
    float rbY = y + bottom;
    float rtX = x + right;
    float rtY = y + top;

    float _ltX = (View::viewWidth / 2 + ltX) / View::viewWidth * 2.0 - 1.0;
    float _ltY = (View::viewHeight / 2 + ltY) / View::viewHeight * 2.0 - 1.0;
    //_x = (worldX / viewWidth) * 2.0f - 1.0f;     等价   _x = (worldX - viewWidth/2) /（viewWidth/2）

    float _lbX = (View::viewWidth / 2 + lbX) / View::viewWidth * 2.0 - 1.0;
    float _lbY = (View::viewHeight / 2 + lbY) / View::viewHeight * 2.0 - 1.0;

    float _rbX = (View::viewWidth / 2 + rbX) / View::viewWidth * 2.0 - 1.0;
    float _rbY = (View::viewHeight / 2 + rbY) / View::viewHeight * 2.0 - 1.0;

    float _rtX = (View::viewWidth / 2 + rtX) / View::viewWidth * 2.0 - 1.0;
    float _rtY = (View::viewHeight / 2 + rtY) / View::viewHeight * 2.0 - 1.0;

    std::vector<float> positions = {
        _ltX, _ltY, 0.0f, 0.0f, // 左上
        _lbX, _lbY, 0.0f, 0.0f, // 坐下
        _rbX, _rbY, 0.0f, 0.0f, // 右下
        _rtX, _rtY, 0.0f, 0.0f  // 右上
    };
    std::vector<float> colors = {
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
    };
    std::vector<float> normals = {
        0.0f,
        0.0f,
        0.0f,
        0.0f,
        0.0f,
        0.0f,
        0.0f,
        0.0f,
        0.0f,
        0.0f,
        0.0f,
        0.0f};
    std::vector<float> uvs = {
        0.0f,
        0.0f,
        0.0f,
        1.0f,
        1.0f,
        1.0f,
        1.0f,
        0.0f,
    };
    std::vector<uint32_t> indices = {
        0, 1, 2,
        0, 2, 3};

    GfxMgr::getInstance()->updateUIObjectVertexs(this->_rendererId, positions, colors, normals, uvs, indices);
}

void UIRenderer::updateColor(float r, float g, float b, float a)
{
    // std::cout << "UIRenderer::updateColor:" << r << " " << g << " " << b << " " << a << std::endl;
    GfxMgr::getInstance()->updateUIObjectColor(this->_rendererId, r, g, b, a);
}
void UIRenderer::updateTexture(std::string texturePath)
{
    GfxMgr::getInstance()->updateUIObjectTexture(this->_rendererId, texturePath);
}
